package com.xxl.job.admin.core.exception;

import com.xxl.job.core.commons.exception.DataxException;
import com.xxl.job.core.commons.response.ReturnCode;
import com.xxl.job.core.commons.response.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常处理器
 *
 * @author xuyue
 * @date 2021-8-4 15:59
 */

@Slf4j
@RestControllerAdvice
public class DataxExceptionHander {

    /**
     * 处理自定义 RRException 异常
     */
    @ExceptionHandler(DataxException.class)
    public ReturnT handleRRException(DataxException ex) {
        return ReturnT.error(ex.getCode(), ex.getMsg());
    }

    /**
     * 处理请求方法不支持异常
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ReturnT handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return ReturnT.error(ReturnCode.REQUEST_METHOD_ERROR.getCode(), ReturnCode.REQUEST_METHOD_ERROR.getMsg() + e.getMethod());
    }

    /**
     * 处理请求参数为空异常
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ReturnT handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        return ReturnT.error(ReturnCode.INVALID_PARAMETER.getCode(), ReturnCode.INVALID_PARAMETER.getMsg() + e.getMessage());
    }

    /**
     * 处理请求参数异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ReturnT handleIllegalArgumentException(IllegalArgumentException e) {
        return ReturnT.error(ReturnCode.INVALID_PARAMETER.getCode(), ReturnCode.INVALID_PARAMETER.getMsg() + e.getMessage());

    }


    /**
     * 处理参数绑定异常
     */
    @ExceptionHandler(BindException.class)
    public ReturnT handleBindException(BindException e) {

        return ReturnT.error(ReturnCode.INVALID_PARAMETER.getCode(), "传参字段映射异常，请检查参数格式：" + e.getObjectName());

    }

    /**
     * 处理int转化异常
     */
    @ExceptionHandler(NumberFormatException.class)
    public ReturnT handleNumberFormatException(NumberFormatException e) {

        return ReturnT.error(ReturnCode.INVALID_PARAMETER.getCode(), "参数转化为int型异常！" + e.getMessage());

    }

    /**
     * 处理传入参数格式异常
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ReturnT handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {

        return ReturnT.error(ReturnCode.INVALID_PARAMETER.getCode(), "传入参数格式不正确(例如：整体没有遵循json格式，某个字段没有传入正确的类型)！");

    }

    /**
     * 路径不存在
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public ReturnT handlerNoFoundException(Exception e) {
        log.error(e.getMessage(), e);
        return ReturnT.error(HttpStatus.NOT_FOUND.value(), "路径不存在，请检查路径是否正确");
    }

    @ExceptionHandler(Exception.class)
    public ReturnT handleException(Exception ex) {
        log.error(ex.getMessage(), ex);
        return ReturnT.error();
    }
}
